/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.dto.LoginOutLogDto;
import com.cmcc.zysoft.infocollection.model.LoginOutLog;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: LoginOutLogPcDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-12 上午9:42:47
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class LoginOutLogPcDao extends HibernateBaseDaoImpl<LoginOutLog, Long> {

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 保存日志.
	 * @param log 日志
	 * @return 返回类型：int
	 */
	@Transactional
	public Long saveLog(LoginOutLog log) {
		return this.save(log);
	}

	/**
	 * 登录日志记录 可根据登录时间查询.
	 * @param rows 分页
	 * @param page 分页
	 * @param startTime 开始时间 
	 * @param endTime 结束时间
	 * @return map 
	 */
	public Map<String, Object> list(int rows, int page, String startTime, String endTime) {
		int offset = (page - 1) * rows;
		String rowSql = "SELECT *,'1' as count FROM tb_c_login_out_log login WHERE 1=1";
		String countSql = "SELECT COUNT(*) FROM tb_c_login_out_log login WHERE 1=1";

		if (StringUtils.hasText(startTime) || StringUtils.hasText(endTime)) {
			rowSql = "SELECT *,count(id) as count FROM tb_c_login_out_log login WHERE 1=1";
			countSql = "select count(*) from (SELECT COUNT(id) as count FROM tb_c_login_out_log login WHERE 1=1";
		}

		if (StringUtils.hasText(startTime)) {
			startTime += " 00:00:00";
			rowSql += " AND login.operate_time >= '" + startTime + "'";
			countSql += " AND login.operate_time >= '" + startTime + "'";
		}
		if (StringUtils.hasText(endTime)) {
			endTime += " 23:59:59";
			rowSql += " AND login.operate_time <= '" + endTime + "'";
			countSql += " AND login.operate_time <= '" + endTime + "'";
		}
		if (StringUtils.hasText(startTime) || StringUtils.hasText(endTime)) {
			rowSql += " group by user_id ";
			countSql += " group by user_id";
		}
		rowSql += " ORDER BY login.operate_time desc limit ?,? ";

		if (StringUtils.hasText(startTime) || StringUtils.hasText(endTime)) {
			countSql += " ) ddd";
		}
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(rowSql, offset, rows);
		int total = this.jdbcTemplate.queryForInt(countSql);
		Map<String, Object> map = new HashMap<>();
		map.put("rows", list);
		map.put("total", total);
		return map;
	}

	/**
	 * 登录日志导出.
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return list
	 * @throws ParseException 异常
	 */
	public List<LoginOutLogDto> getListForExcel(String startTime, String endTime) throws ParseException {
		String rowSql = "SELECT * FROM tb_c_login_out_log login WHERE 1=1";
		if (StringUtils.hasText(startTime)) {
			startTime += " 00:00:00";
			rowSql += " AND login.operate_time >= '" + startTime + "'";
		}
		if (StringUtils.hasText(endTime)) {
			endTime += " 23:59:59";
			rowSql += " AND login.operate_time <= '" + endTime + "'";
		}
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(rowSql);
		List<LoginOutLogDto> dtoList = new ArrayList<LoginOutLogDto>();
		for (Map<String, Object> map : list) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			LoginOutLogDto loginOutLogDto = new LoginOutLogDto();
			loginOutLogDto.setUserName(map.get("user_name") == null ? "" : map.get("user_name").toString());
			loginOutLogDto.setOperateType(("1").equals(map.get("operate_type") == null ? "" : map.get("operate_type")
					.toString()) ? "登录" : "注销");
			loginOutLogDto.setOperateTime(dateFormat.parse(map.get("operate_time") == null ? "" : map.get(
					"operate_time").toString()));
			loginOutLogDto.setLoginIp(map.get("login_ip") == null ? "" : map.get("login_ip").toString());
			loginOutLogDto.setClientType(("1").equals(map.get("client_type") == null ? "" : map.get("client_type")
					.toString()) ? "电脑" : "手机");
			loginOutLogDto.setLoginResult(("1").equals(map.get("login_result") == null ? "" : map.get("login_result")
					.toString()) ? "成功" : "失败");
			loginOutLogDto.setLogContent(map.get("log_content") == null ? "" : map.get("log_content").toString());
			loginOutLogDto.setTimeConsuming(map.get("time_consuming") == null ? "" : map.get("time_consuming")
					.toString());
			dtoList.add(loginOutLogDto);
		}
		return dtoList;
	}
	
	/**
	 * 首页-最新动态.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> latestDynamic(String companyId) {
		String sql = 
				" SELECT a.id AS dynamicId, b.real_name, '系统' AS content, DATE_FORMAT(a.operate_time, '%c月%e日 %H:%i') AS 'date', a.operate_time AS operateTime, '登录' AS opterate, '0' AS 'type' " +
				" FROM tb_c_login_out_log a, tb_c_system_user b " +
				" WHERE a.user_id = b.user_id AND a.login_result = '1' AND b.company_id = ? " +
				" UNION ALL " +
				" SELECT c.resource_id  AS dynamicId, d.real_name, c.name, DATE_FORMAT(c.add_date, '%c月%e日 %H:%i') AS 'date', c.add_date AS operateTime, CONCAT((CASE c.data_flag WHEN '0' THEN '新增了' WHEN '1' THEN '更新了' ELSE '删除了' END), '商户') AS optType, '1' AS 'type' " +
				" FROM tb_b_resource c, tb_c_system_user d " +
				" WHERE c.add_man = d.user_id AND c.del_flag = '0' AND d.company_id = ? " +
				" ORDER BY operateTime DESC " +
				" LIMIT 0, 5 ";
		return this.jdbcTemplate.queryForList(sql, companyId, companyId);
	}
}